package strategy

import (
	"gvb_server/global"
	"io"
	"mime/multipart"
	"os"
	"path"
)

type LocalUploadStrategy struct {
}

func (LocalUploadStrategy) Upload(file *multipart.FileHeader) (filePath string, err error) {
	filePath = path.Join(global.Config.Local.StorePath, file.Filename)
	openFile, err := file.Open()
	if err != nil {
		global.Log.Errorf("open file error: %s", err)
		return "", err
	}
	byteData, err := io.ReadAll(openFile)
	err = os.WriteFile(filePath, byteData, os.ModePerm)
	if err != nil {
		global.Log.Errorf("本地保存文件失败: %s", file.Filename)
		return "", err
	}
	return filePath, nil
}
